package com.iplant.wms.controller.bill;

import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.controller.BasicController;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.wms.service.WMSService;
import com.iplant.wms.service.po.bill.WMSSalesDelivery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author PengYouWang
 * 2022-9-19 21:27:05
 */
@RestController
@RequestMapping("/api/WMSSalesDelivery")
public class WMSSalesDeliveryController  extends BasicController<WMSSalesDelivery> {
    private static final Logger logger = LoggerFactory.getLogger(WMSSalesDeliveryController.class);

    @Autowired
    WMSService mWMSService;

    public WMSSalesDeliveryController( ) {
        super(WMSSalesDelivery.class);

    }

    /**
     * 申请发货
     */
    @PostMapping("/ApplyForShipment")
    public Object ApplyForShipment(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult;
        try {
            BMSEmployee wLoginUser = GetSession(request);

            int wWMSSalesDeliveryID = StringUtils.parseInt(wParam.get("WMSSalesDeliveryID"));
            if (wWMSSalesDeliveryID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = mWMSService.WMS_ApplyForShipment(wLoginUser, wWMSSalesDeliveryID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/Update")
    public Object Update(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult;
        try {
            BMSEmployee wLoginUser = GetSession(request);


            int wWMSSalesDeliveryID = StringUtils.parseInt(wParam.get("WMSSalesDeliveryID"));
            if (wWMSSalesDeliveryID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = mWMSService.WMS_ApplyForShipment(wLoginUser, wWMSSalesDeliveryID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }




    /**
     * 开始分拣
     */
    @PostMapping("/StartSorting")
    public Object StartSorting(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult;
        try {
            BMSEmployee wLoginUser = GetSession(request);

            WMSSalesDelivery wWMSSalesDelivery = CloneTool.Clone(wParam.get("data"), WMSSalesDelivery.class);

            ServiceResult<WMSSalesDelivery> wServiceResult = mWMSService.WMS_SalesDeliveryStartSorting(wLoginUser,
                    wWMSSalesDelivery);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 结束分拣
     */
    @PostMapping("/FinishSorting")
    public Object FinishSorting(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult;
        try {
            BMSEmployee wLoginUser = GetSession(request);

            WMSSalesDelivery wWMSSalesDelivery = CloneTool.Clone(wParam.get("data"), WMSSalesDelivery.class);
            if (wWMSSalesDelivery == null || wWMSSalesDelivery.ID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<WMSSalesDelivery> wServiceResult = mWMSService.WMS_SalesDeliveryFinishSorting(wLoginUser,
                    wWMSSalesDelivery);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 确认出库
     */
    @PostMapping("/ConfirmOutOfStock")
    public Object ConfirmOutOfStock(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult;
        try {
            BMSEmployee wLoginUser = GetSession(request);

            WMSSalesDelivery wWMSSalesDelivery = CloneTool.Clone(wParam.get("data"), WMSSalesDelivery.class);
            if (wWMSSalesDelivery == null || wWMSSalesDelivery.ID <= 0
//                    || StringUtils.isEmpty(wWMSSalesDelivery.U8_BillCode)
            ) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<WMSSalesDelivery> wServiceResult = mWMSService.WMS_SalesDeliveryConfirmOutOfStock(wLoginUser,
                    wWMSSalesDelivery);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);

                // 推送U8（此处是审核销售发货单）(已废除)
//                ServiceResult<U8ApiSendResult> wU8Result = mU8ApiService.U8Api_AuditSalesStoreOut(wLoginUser, wWMSSalesDelivery.U8_BillCode);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 提交发货单
     */
    @GetMapping("/SubmitInvoice")
    public Object SubmitInvoice(HttpServletRequest request) {
        Object wResult;
        try {

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wWMSSalesDeliveryID = StringUtils.parseInt(request.getParameter("WMSSalesDeliveryID"));

            if (wWMSSalesDeliveryID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<WMSSalesDelivery> wServiceResult = mWMSService.WMS_SalesDeliverySubmitInvoice(wLoginUser,
                    wWMSSalesDeliveryID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

}
